import Vue from 'vue'
import VueRouter from 'vue-router'

// App_old 当作 xflow 作为内容主页
import xflow from "../views/App_old.vue"

// 登陆
import Login from "../views/Login.vue"
import CoreLogin from '../components/login/CoreLogin.vue'
import CoreRegister from '../components/login/CoreRegister.vue'

// 首页
import X_PageShow from "../views/X_PageShow.vue"

// 笔记
import X_Note from "../views/X_Note.vue"
import WatchNote from '../components/note/WatchNote.vue'

// 视频
import X_Video from "../views/X_Video.vue"
import WatchVideo from '../components/video/WatchVideo.vue'

// 我的
import X_AboutMe from "../views/X_AboutMe.vue"

// 后台管理 admin
// // 管理员 利用旧的Publish组件
import admin from '../views/Admin.vue'
// import PubNote from '../components/publish/PubNote.vue'
import ManageNote from '../components/admin/ManageNote.vue'
import ManageVideo from '../components/admin/ManageVideo.vue'
import ManageUser from '../components/admin/ManageUser.vue'
import PubNote from '../components/admin/PubNote.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: "/login"
  },
  // 登录
  {
    path: '/login',
    component: Login,
    children: [
      {
        path: '/',
        redirect: "coreLogin"
      },
      {
        path: 'coreLogin',
        component: CoreLogin
      },
      {
        path: 'coreRegister',
        component: CoreRegister
      }
    ]
  },
  // App_old-主页内容，根目录是xflow
  {
    path: '/xflow',
    component: xflow,
    children: [
      {
        path: '/',
        redirect:"X_PageShow"
      },
      {
        path: 'X_Note',
        component: X_Note,
      },
      {
        path: 'X_Video',
        component: X_Video
      },
      {
        path: 'X_PageShow',
        component: X_PageShow
      },
      {
        path: 'X_AboutMe',
        component: X_AboutMe
      },
      {
        path: 'WatchNote',
        component: WatchNote
      },
      {
        path: 'WatchVideo',
        component: WatchVideo
      }
    ]
  },
  // 管理员
  {
    path: '/Admin',
    component: admin,
    children: [
      {
        path: '/',
        redirect:"ManageNote"
      },
      {
        path: 'ManageNote',
        component: ManageNote
      },
      {
        path: 'ManageVideo',
        component: ManageVideo
      },
      {
        path: 'ManageUser',
        component: ManageUser
      },
      {
        path: 'PubNote',
        component: PubNote
      }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  if (to.path === '/login/coreLogin') {
    return next()
  }
  if (to.path === '/login/coreRegister') {
    return next()
  }
  if (to.path === '/xflow/X_PageShow') {
    return next()
  }

  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }

  return next()

})

export default router

